package com.igoal.dto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.igoal.entity.MemberMail;
import com.igoal.utils.BaseSynchronizeAttributeWithOther;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;
import com.igoal.web.Constants;

public class MemberMailDTO extends BaseSynchronizeAttributeWithOther<MemberMail> {
	
	private Long id;
	private String name;
	private String subject;
	private String body;
	private String date;
	private List<String> groups;
	private String groupNames;
	private boolean igoalDB;
	private boolean marketingDB;
	private Integer sendingStatus;
	private Integer priority;
	private Long totalEmailSent;
	private Long totalEmail;
	
	public MemberMailDTO() {
		sendingStatus = Constants.SCHEDULED_PENDING;
		totalEmail = 0l;
		totalEmailSent = 0l;
		groups = new ArrayList<String>();
		igoalDB = false;
		marketingDB = false;
		name = "";
		subject = "";
		body = "";
		date = "";
		priority = 4;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public boolean isIgoalDB() {
		return igoalDB;
	}
	public boolean getIgoalDB() {
		return igoalDB;
	}
	public void setIgoalDB(boolean igoalDB) {
		this.igoalDB = igoalDB;
	}
	public boolean isMarketingDB() {
		return marketingDB;
	}
	public boolean getMarketingDB() {
		return marketingDB;
	}
	public void setMarketingDB(boolean marketingDB) {
		this.marketingDB = marketingDB;
	}
	public Integer getSendingStatus() {
		return sendingStatus;
	}
	public void setSendingStatus(Integer sendingStatus) {
		this.sendingStatus = sendingStatus;
	}
	public Integer getPriority() {
		return priority;
	}
	public void setPriority(Integer priority) {
		this.priority = priority;
	}
	public Long getTotalEmailSent() {
		return totalEmailSent;
	}
	public void setTotalEmailSent(Long totalEmailSent) {
		this.totalEmailSent = totalEmailSent;
	}
	public Long getTotalEmail() {
		return totalEmail;
	}
	public void setTotalEmail(Long totalEmail) {
		this.totalEmail = totalEmail;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public List<String> getGroups() {
		return groups;
	}
	public void setGroups(List<String> groups) {
		this.groups = groups;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getGroupNames() {
		return groupNames;
	}
	public void setGroupNames(String groupNames) {
		this.groupNames = groupNames;
	}
	@SynchronizeAttributeWithOther(direction=SynchronizeDirection.FROM, value="scheduledDate")
	public void initScheduledDate(Object entity){
		if(entity != null && entity instanceof Date){
			Date dateTemp = (Date) entity;
			date = CalendarUtils.formatDate(dateTemp, "dd/MM/yyyy");
		}
	}
	
	@SynchronizeAttributeWithOther(direction=SynchronizeDirection.TO, value="scheduledDate")
	public Date copyScheduledDate(){
		Date scheduledDateTime = CalendarUtils.parseDate(date, "dd/MM/yyyy");
		if(scheduledDateTime == null){
			scheduledDateTime = new Date();
		}
		
		return scheduledDateTime;
	}
	
	@SynchronizeAttributeWithOther(direction=SynchronizeDirection.TO, value="receptionGroups")
	public String copyReceptionGroups(){
		String receptionGroups = "";
		for(String group : groups){
			if(StringUtils.isNotBlank(receptionGroups)){
				receptionGroups += ",";
			}
			receptionGroups += group;
		}
		
		return receptionGroups;
	}
	
	@SynchronizeAttributeWithOther(direction=SynchronizeDirection.FROM, value="receptionGroups")
	public void initReceptionGroups(Object entity){
		if(entity != null && entity instanceof String){
			String receptionGroups = (String) entity;
			String[] gs = receptionGroups.split(",");
			groups = Arrays.asList(gs);
			groupNames = receptionGroups;
		}
	}
}
